﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Messaging;
using System.Collections;


public partial class admin_permisos : System.Web.UI.Page
{
    rUser ruser;
    rUser userinfo;
    int contclient;
    int contvendor;
    Table tableclient, tablevendor, tablaPanel;
    Table tablaPermit = null;
    TableRow rowPage;
    string puser="";
    int rolindex = 0;
    RE_GenericBean rpage = new RE_GenericBean();
    //AjaxControlToolkit.TabPanel[] tPanels;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
        //Si es la primera vez que entra
        ruser = (rUser)Session["userid"];
       
        if (!Page.IsPostBack)
        {
            rpage.intC1 = 0;
            rpage.intC2 = 0;
            Session["rpermitpage"] = rpage;
           // orden = int.Parse(Request.QueryString["User"].ToString());
            puser = Request.QueryString["Id"].ToString();
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            string usuario = ruser.getUsuarioId();
            //string info = serv.getTodosDatosUsuario(ruser.getUsuarioId());
            string info = service.getTodosDatosUsuario(puser);
            userinfo = new rUser();
            userinfo = Utility.getRUser(info);
            Session["userinfo"] = userinfo;
            userinfo = (rUser)(Session["userinfo"]);
            fillcompanies();
            fillaccounts();
            fillperfiles();
            fillviewdefault();
            fillAccess(0, 0);
            //paintRed();
            int bandera = 0;
            foreach (RE_GenericBean obj in userinfo.perfiles)
            {
                if (obj.intC1 == 1 && bandera == 0) { fillclientprices(); bandera = 1; PVendor.Visible = false; Pcoverage.Visible = true; }
                else if (bandera == 0) { fillvendorprices(); bandera = 1; PClient.Visible = false; Pcoverage.Visible = true; } else { Pcoverage.Visible = false; PClient.Visible = false; PVendor.Visible = false; }
            }
            lnk_activate.Visible = false;
            lnk_deactivate.Visible = false;

            if (userinfo.estado == 1)
            {
                lnk_deactivate.Visible = true;

            }
            else {
                lnk_activate.Visible = true;
            }
            fillstates();
            fillcounties();
            fillproductviews();
            name.Text = userinfo.getNombre().ToUpperInvariant() + " " + userinfo.getApellido().ToUpperInvariant();
            logo.ImageUrl = userinfo.getLogo();
            ChkListEmployee.Enabled = false;
            rolindex= ChkListEmployee.SelectedIndex;
            Session["rolindex"] = rolindex;
        }
        //no es la primera vez que entra
        else {
            ruser = (rUser)Session["userid"];
            userinfo= (rUser)Session["userinfo"];
            foreach (RE_GenericBean obj in userinfo.perfiles)
            {
                if (obj.intC1 == 1) {
                    tableclient = (Table)Session["tbl_clientprices"];
                    PClientPrices.Controls.Add(tableclient);
                    contclient = tableclient.Rows.Count * 5;
                }
                else {
                    tablevendor = (Table)Session["tbl_vendorprices"];
                    PVendorPrices.Controls.Add(tablevendor);
                    contvendor = tablevendor.Rows.Count * 3;
                }
            }
            rpage = (RE_GenericBean)Session["rpermitpage"];
            if (rpage != null)
            {
                fillAccess(rpage.intC1, rpage.intC2);
            }
            string aux = (string)Session["PageID"];
            if (aux != null)
            {
                crearPagPermisos(aux);
                Session["PageID"] = null;
            }
        }
    }

    private void paintRed()
    {
        /*long rolacc = long.Parse(userinfo.getPermiso().ToString());
        foreach (ListItem item in ChkListAccess.Items)
        {
            long p = long.Parse(item.Value);
            long pp = p & rolacc;
            if (pp == p)
            {
                //item.Enabled = false;
                item.Attributes.Add("Style", "color: red;");
            }
        }
        long rolres = long.Parse(userinfo.restrictedrol.ToString());
        foreach (ListItem item in ChkListRestricted.Items)
        {
            long p = long.Parse(item.Value);
            long pp = p & rolres;
            if (pp == p)
            {
                //item.Enabled= false;
                item.Attributes.Add("Style", "color: red;");
            }
        }*/
    }

    private void fillproductviews()
    {
        DataSet dst = DB.GetProducts();
        chkfilterview.Items.Clear();
        ListItem li = new ListItem();
        if (dst != null)
        {
            for (int i = 0; i <= (dst.Tables[0].Rows.Count - 1); i++)
            {
               
                    li = new ListItem();
                    li.Text = dst.Tables[0].Rows[i]["Initial"].ToString();
                    li.Value = dst.Tables[0].Rows[i]["Product_id"].ToString();
                    chkfilterview.Items.Add(li);
            }
            //ahorita marco lo que trae where3
            string where3 = userinfo.getWhere3();

            foreach (ListItem item in chkfilterview.Items)
            {
                string cadena = "product_id != " + item.Value + " ";
                int indice = where3.IndexOf(cadena);
                if (indice != -1)
                {
                    item.Selected = false;
                }
                else {
                    item.Selected = true;
                }
            }
        }
    }

    private void fillstates()
    {
            DataSet dst = DB.GetStates();
            ChkListState.Items.Clear();
            cbxstate.Items.Clear();
            ListItem li = new ListItem();
            li.Text = "Select One";
            li.Value = "Select One";
            cbxstate.Items.Add(li);
            if (dst != null)
            {
                for (int i = 0; i <= (dst.Tables[0].Rows.Count - 1); i++)
                {
                    if (dst.Tables[0].Rows[i]["enable"].ToString().Equals("1"))
                    {
                        li= new ListItem();
                        li.Text= dst.Tables[0].Rows[i]["name"].ToString();
                        li.Value= dst.Tables[0].Rows[i]["state_id"].ToString();

                        cbxstate.Items.Add(li);
                        foreach (RE_GenericBean obj in userinfo.states)
                        {
                            string b = obj.intC1.ToString();
                            if (li.Value.ToUpper().Equals(b))
                            {
                                li.Selected = true;
                            }
                        }
                        ChkListState.Items.Add(li);
                    }
                }
            }
    }

    private void fillcompanies()
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getCompanies();

            if (!rxml.Equals("") || rxml != null)
            {
                DataSet dsclient = Utility.getCompanies(rxml);

                CKListCompanies.DataSource = dsclient;
                CKListCompanies.DataTextField = "com_nombre";
                CKListCompanies.DataValueField = "com_id";
                CKListCompanies.DataBind();

                ChkListCompanyAcc.DataSource = dsclient;
                ChkListCompanyAcc.DataTextField = "com_nombre";
                ChkListCompanyAcc.DataValueField = "com_id";
                ChkListCompanyAcc.DataBind();

                foreach (ListItem item in CKListCompanies.Items)
                {
                    foreach (RE_GenericBean obj in userinfo.companies)
                    {
                        string b = obj.strC1.ToUpper();
                        if (item.Text.ToUpper().Equals(obj.strC1.ToUpper()))
                        {
                            item.Selected = true;
                        }
                    }
                }

                //ahorita marco lo que trae where2
                string where2 = userinfo.getWhere2();
                
                foreach (ListItem item in ChkListCompanyAcc.Items)
                {
                    string cadena = "company != " + item.Value + " ";
                    int indice = where2.IndexOf(cadena);
                    if (indice != -1)
                    {
                        item.Selected = false;
                    }
                    else {
                        item.Selected = true;
                    }
                }
            }
        }catch(Exception ex){
            Utility.writeError("Error al obtener Companies WEBSERVICE Permiso.aspx "+ex.Message+" "+ex.StackTrace );
        }
    }
    private void fillaccounts()
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getAccounts();

            if (!rxml.Equals("") || rxml != null)
            {
                DataSet dsclient = Utility.getAccounts(rxml);

                CKListAccounts.DataSource = dsclient;
                CKListAccounts.DataTextField = "acc_nombre";
                CKListAccounts.DataValueField = "acc_id";
                CKListAccounts.DataBind();

                foreach (ListItem item in CKListAccounts.Items)
                {
                    foreach (RE_GenericBean obj in userinfo.accounts)
                    {
                        
                        if (item.Text.ToUpper().Equals(obj.strC1.ToUpper()))
                        {
                            item.Selected = true;
                        }
                    }
                }
            }

        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener Companies WEBSERVICE Permiso.aspx " + ex.Message + " " + ex.StackTrace);
        }
    }
    private void fillperfiles()
   {
        try
        {
            string rxml = null;

            string group = userinfo.getGroup().ToUpper();
            if (group == "CLIENT")
            {
                chkClient.Checked = true;
                chkClient.Attributes.Add("Style", "color: #000000;");
            }
            else if (group == "VENDOR")
            {
                chkVendor.Checked = true;
                chkVendor.Attributes.Add("Style", "color: #000000;");
            }
            else if (group == "EMPLOYEE")
            {
                chkEmployee.Checked = true;
                chkEmployee.Attributes.Add("Style", "color: #000000;");
            }
                            
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getPerfiles(1,1);


            if (!rxml.Equals("") || rxml != null)
            {
                DataSet dsclient = Utility.getPerfiles(rxml);

                ChkListEmployee.DataSource = dsclient;
                ChkListEmployee.DataTextField = "per_nombre";
                ChkListEmployee.DataValueField = "per_id";
                ChkListEmployee.DataBind();
                int i=0;
                foreach (ListItem item in ChkListEmployee.Items)
                {

                    foreach (RE_GenericBean obj in userinfo.perfiles)
                    {
                        if (item.Text.ToUpper().Equals(obj.strC1.ToUpper()))
                        {
                            item.Selected = true;
                            //item.Attributes.Add("font-weight","900");
                            item.Attributes.Add("Style", "color: #000000;");
                            
                            Session["oldperfil"] = item.Value;
                            chkClient.Checked = false;
                            chkClient.Attributes.Add("Style", "color: #525663;");
                            
                            if (obj.intC1 == 1) { chkClient.Checked = true; chkClient.Attributes.Add("Style", "color: #000000;"); }
                            else if (obj.intC1 == 2) { chkVendor.Checked = true; chkVendor.Attributes.Add("Style", "color: #000000;"); }
                            else { chkEmployee.Checked = true; chkEmployee.Attributes.Add("Style", "color: #000000;"); PClient.Visible = false; PVendor.Visible = false; }
                        }
                    }
                }

            }
        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener Companies WEBSERVICE Permiso.aspx " + ex.Message + " " + ex.StackTrace);

        }
    }

    private void fillviewdefault()
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.UsuariosPorPerfil(1);


            if (!rxml.Equals("") || rxml != null)
            {
                DataSet dsclient =   Utility.getUsuariosporPerfil(rxml);

                ChkListViewDe.DataSource = dsclient;
                ChkListViewDe.DataTextField = "user";
                ChkListViewDe.DataValueField = "user";
                ChkListViewDe.DataBind();

                //ahorita marco lo que trae where1
                string where = userinfo.getWhere();

                foreach (ListItem item in ChkListViewDe.Items)
                {
                    string cadena = "client_id != '" + item.Value + "' ";
                    int indice = where.IndexOf(cadena);
                    if (indice != -1)
                    {
                        item.Selected = false;
                    }
                    else {
                        item.Selected = true;
                    }
                }

            }
        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener Companies WEBSERVICE Permiso.aspx " + ex.Message + " " + ex.StackTrace);

        }
    }

    private void fillAccess(int pgnivel, int pgnum)
    {
        try
        {
            string rxml = null, rxml2 = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml2 = service.getPagesView(1);

            if (!rxml2.Equals("") || rxml2 != null)
            {
                DataSet dsPages = Utility.getPagesView(rxml2);
                Session["dsPermitView"] = dsPages;
                rxml = service.getAppPages(1);

                if (!rxml.Equals("") || rxml != null)
                {
                    DataSet dsclient = Utility.getPermitPage(rxml);
                    if (dsclient != null)
                    {
                        Session["dsPermitPages"] = dsclient;

                        //tPanels = new AjaxControlToolkit.TabPanel[dsclient.Tables[0].Rows.Count];
                        if (dsclient.Tables[0].Columns.Count != 0)
                        {
                            Punaccess.Controls.Clear();
                            tablaPanel = new Table();
                            tablaPanel.Width = 720;
                            tablaPanel.CellPadding = 5;
                            tablaPanel.HorizontalAlign = HorizontalAlign.Left;
                            tablaPanel.Font.Size = 9;
                            tablaPanel.Font.Name = "Arial";
                            tablaPanel.BorderStyle = BorderStyle.Ridge;
                            tablaPanel.BorderColor = System.Drawing.Color.Black;
                            tablaPanel.BorderWidth = 2;
                            tablaPanel.Attributes.Add("Style", "padding-left:30px");

                            if (pgnivel == 0)
                            {
                                for (int j = 0; j < dsPages.Tables[0].Rows.Count; j++)
                                {
                                    if (int.Parse(dsPages.Tables[0].Rows[j]["type"].ToString()) == 1)
                                    {
                                        for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                                        {
                                            if (int.Parse(dsPages.Tables[0].Rows[j]["page_id"].ToString()) == int.Parse(dsclient.Tables[0].Rows[i]["id_page"].ToString()))
                                            {
                                                loadImaPages((j + 1), dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString());
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        loadImaPages((j + 1), ("1_" + dsPages.Tables[0].Rows[j]["id"].ToString()), dsPages.Tables[0].Rows[j]["name"].ToString(), dsPages.Tables[0].Rows[j]["url"].ToString());
                                    }
                                }
                            }
                            else
                            {
                                int contp = 0;
                                for (int j = 0; j < dsPages.Tables[pgnivel].Rows.Count; j++)
                                {
                                    string pageid = "pg" + pgnivel + "_id";
                                    if (int.Parse(dsPages.Tables[pgnivel].Rows[j][pageid].ToString()) == pgnum)
                                    {
                                        contp++;
                                        if (int.Parse(dsPages.Tables[pgnivel].Rows[j]["type"].ToString()) == 1)
                                        {
                                            for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                                            {
                                                if (int.Parse(dsPages.Tables[pgnivel].Rows[j]["page_id"].ToString()) == int.Parse(dsclient.Tables[0].Rows[i]["id_page"].ToString()))
                                                {
                                                    loadImaPages(contp, dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString());
                                                    break;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            loadImaPages(contp, ("2_" + dsPages.Tables[pgnivel].Rows[j]["id"].ToString()), dsPages.Tables[pgnivel].Rows[j]["name"].ToString(), dsPages.Tables[pgnivel].Rows[j]["url"].ToString());
                                        }
                                    }
                                }
                            }
                        }
                        Punaccess.Controls.Add(tablaPanel);
                        Punaccess.Controls.Add(lnk_back);
                        //tPanels[i] = tabPanel;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener Companies WEBSERVICE Permiso.aspx " + ex.Message + " " + ex.StackTrace);
        }
        
/*        for (int i = 0; i < tPanels.Count(); i++)
        {
            TabContainer1.Controls.Add(tPanels[i]);
        }

        AjaxControlToolkit.TabPanel aux = (AjaxControlToolkit.TabPanel)TabContainer1.FindControl("Page2");
        if (aux != null)
        {
            string x = aux.HeaderText.ToString();
        }
*/ 
    }
    private void loadImaPages(int contPage, string idPage, string namePage, string imaPage)
    {
        if (contPage == 1)
        {
            rowPage = new TableRow();
            tablaPanel.Controls.Add(rowPage);
        }
        if ((contPage % 4) == 0)
        {
            rowPage.Cells.Add(oldRowIma(contPage, idPage, namePage, imaPage));
            tablaPanel.Controls.Add(rowPage);
            rowPage = new TableRow();
        }
        else
        {
            rowPage.Cells.Add(oldRowIma(contPage, idPage, namePage, imaPage));
            tablaPanel.Controls.Remove(rowPage);
            tablaPanel.Controls.Add(rowPage);
        }
    }

    private TableCell oldRowIma(int contPage, string idPage, string namePage, string imaPage)
    {
        TableCell result = new TableCell();
        
        Table table = new Table();
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Width = 120;
        cell1.Height = 60;
        cell1.HorizontalAlign = HorizontalAlign.Center;
        ImageButton imaButton = new ImageButton();
        imaButton.ID = "bt_" + idPage;
        imaButton.BorderWidth = 0;

        string auxOver = imaPage.Replace(".", "2.");
        imaButton.Attributes.Add("onmouseout", "this.src='../img/ImaPages/" + imaPage + "'");
        imaButton.Attributes.Add("onmouseover", "this.src='../img/ImaPages/" + auxOver + "'");

        if (!imaPage.Equals(""))
        {
            imaButton.ImageUrl = "../img/ImaPages/" + imaPage;
        }
        imaButton.Click += new ImageClickEventHandler(imaButton_Click);
        cell1.Attributes.Add("Style", "background: url(../img/ImaPages/base.png) no-repeat center bottom;");
        cell1.Controls.Add(imaButton);
        row1.Cells.Add(cell1);
        table.Controls.Add(row1);

        TableRow row2 = new TableRow();

        TableCell cell2 = new TableCell();
        cell2.Width = 120;
        cell2.Height = 20;
        cell2.HorizontalAlign = HorizontalAlign.Center;
        Label imaLabel = new Label();
        imaLabel.Text = namePage;
        imaLabel.Font.Bold = true;
        cell2.Controls.Add(imaLabel);
        row2.Cells.Add(cell2);
        table.Controls.Add(row2);

        result.Controls.Add(table);
        return result;
    }

    void imaButton_Click(object sender, ImageClickEventArgs e)
    {
        Session["PageID"] = null;
        ImageButton ibt = (ImageButton)sender;
        string ID = ibt.ID;
        string[] pID = ID.Split('_');
        if (pID.Length < 3)
        {
            crearPagPermisos(ID);
            Session["PageID"] = ID;
            bthide_ModalPopupExtender.Show();
        }
        else
        {
            rpage.intC1 = int.Parse(pID[1]);
            rpage.intC2 = int.Parse(pID[2]);
            Session["rpermitpage"] = rpage;
            fillAccess(rpage.intC1, rpage.intC2);
            lnk_back.Visible = true;
        }
    }

    private void crearPagPermisos(string pageID)
    {
        DataSet dsclient = (DataSet)Session["dsPermitPages"];
        
        string[] pID = pageID.Split('_');
        if (dsclient != null)
        {
            for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
            {
                if (dsclient.Tables[0].Rows[i]["id_page"].ToString() == pID[1].Trim())
                {
                    if (puppermit.Controls.Contains(tablaPermit))
                        puppermit.Controls.Remove(tablaPermit);
                    tablaPermit = new Table();
                    //tablaPermit.Width = 450;
                    tablaPermit.CellPadding = 5;
                    tablaPermit.HorizontalAlign = HorizontalAlign.Left;
                    tablaPermit.Font.Size = 9;
                    tablaPermit.Font.Name = "Arial";
                    TableRow row1 = new TableRow();
                    row1.Cells.Add(rowPagina(dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString()));
                    tablaPermit.Controls.Add(row1);
                    //TableRow rows = new TableRow();

                    puppermit.Controls.Add(tablaPermit);
                    newSection(dsclient.Tables[0].Rows[i]["id_page"].ToString());
                    newPage(dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["permit"].ToString(), dsclient);
                    
                    //tablaPermit.Controls.Add(rows);
                    TableRow rows2 = new TableRow();
                    TableCell cell = new TableCell();
                    cell.Controls.Add(lnk_savePermit);
                    cell.Controls.Add(lnk_cancelPermit);
                    rows2.Cells.Add(cell);
                    rows2.Attributes.Add("Style", "text-align:center");
                    tablaPermit.Controls.Add(rows2);
                    tablaPermit.ID = "tabla" + dsclient.Tables[0].Rows[i]["id_page"].ToString();
                    //tablaPermit.Attributes.Add("Style", "margin:7px; width:410px");
                    break;
                }
            }
            
            puppermit.BorderColor = System.Drawing.Color.Black;
            puppermit.BorderStyle = BorderStyle.Ridge;

            mostrarPermisos(0, dsclient);
            mostrarPermisos(1, dsclient);
        }
    }

    private TableCell rowPagina(string idPage, string namePage, string imaPage)
    {
        TableCell result = new TableCell();

        Table table = new Table();
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Width = 60;
        cell1.Height = 60;
        cell1.HorizontalAlign = HorizontalAlign.Left;
        ImageButton imageButton = new ImageButton();
        imageButton.ID = "btn_" + idPage;
        imageButton.BorderWidth = 0;

        string auxOver = imaPage.Replace(".", "2.");
        imageButton.Attributes.Add("onmouseout", "this.src='../img/ImaPages/" + imaPage + "'");
        imageButton.Attributes.Add("onmouseover", "this.src='../img/ImaPages/" + auxOver + "'");

        if (!imaPage.Equals(""))
        {
            imageButton.ImageUrl = "../img/ImaPages/" + imaPage;
        }
        cell1.Controls.Add(imageButton);
        row1.Cells.Add(cell1);
        table.Controls.Add(row1);

        TableCell cell2 = new TableCell();
        cell2.Width = 350;
        cell2.Height = 20;
        cell2.HorizontalAlign = HorizontalAlign.Left;
        Label imaLabel = new Label();
        imaLabel.Text = namePage;
        imaLabel.Font.Bold = true;
        cell2.Controls.Add(imaLabel);
        cell2.Font.Size = 14;
        cell2.Font.Name = "Arial";
        row1.Cells.Add(cell2);

        TableRow row2 = new TableRow();
        TableCell cell3 = new TableCell();
        cell3.Controls.Add(allaccess);
        allaccess.Visible = false;
        cell3.ColumnSpan = 2;
        cell3.Font.Size = 9;
        cell3.Font.Name = "Arial";
        row2.Cells.Add(cell3);
        
        table.Controls.Add(row2);

        result.Controls.Add(table);
        return result;
    }

    public void newPage(string id_parent, string name_page, string per_page, DataSet oppPage)
    {
        DataSet section = null;
        section = DB.getSectionPage(id_parent);
        // Devuelve una tabla para cada pagina
        //TableCell result = new TableCell();

        // Permisos de la pagina
        TableRow rowPer = new TableRow();

        TableCell cellblank = new TableCell();
        cellblank.HorizontalAlign = HorizontalAlign.Left;
        cellblank.Width = 140;
        //cellblank.BorderStyle = BorderStyle.Groove;
        //cellblank.BorderWidth = 2;
        Label lnamepage = new Label();
        lnamepage.Text = name_page;
        cellblank.Controls.Add(lnamepage);
        rowPer.Cells.Add(cellblank);

        TableCell cellPer = new TableCell();
        cellPer.HorizontalAlign = HorizontalAlign.Left;
        RadioButtonList rl1 = new RadioButtonList();
        rl1.ID = "page" + id_parent;
        rl1.Items.Add("Unlimited");
        rl1.Items.Add("View Only");
        rl1.Items.Add("Restricted");
        //rl1.Font.Bold = true;

        // Diseño y aparencia de la opcion
        /*rl1.Items[0].Attributes.Add("onmouseover", "this.style.color='green';");
        rl1.Items[0].Attributes.Add("onmouseout", "this.style.color='black';");
        rl1.Items[1].Attributes.Add("onmouseover", "this.style.color='#FA7E02';");
        rl1.Items[1].Attributes.Add("onmouseout", "this.style.color='black';");
        rl1.Items[2].Attributes.Add("onmouseover", "this.style.color='red';");
        rl1.Items[2].Attributes.Add("onmouseout", "this.style.color='black';");*/
        rl1.RepeatDirection = RepeatDirection.Horizontal;

        cellPer.Controls.Add(rl1);
        rowPer.Cells.Add(cellPer);

        Table tableG = (Table)Accordion1.FindControl("TableSecGeneral");
        if (tableG != null)
            tableG.Controls.Add(rowPer);
        int contOpp = 0;
        TableRow rowOpp = null;
        // Opciones de la pagina
        for (int i = 0; i < oppPage.Tables[1].Rows.Count; i++)
        {
            if (oppPage.Tables[1].Rows[i]["id_page"].ToString() == id_parent)
            {
                if (section.Tables[0].Rows.Count > 1)
                {
                    contOpp++;
                    if ((contOpp % 2) != 0)
                        rowOpp = new TableRow();
                }
                else { rowOpp = new TableRow(); }

                // Nueva celda nombre opcion
                TableCell cellOpp = new TableCell();
                cellOpp.Width = 140;
                cellOpp.HorizontalAlign = HorizontalAlign.Left;
                Label lOpp = new Label();
                lOpp.Width = 140;
                lOpp.Text = oppPage.Tables[1].Rows[i]["name_option"].ToString();
                lOpp.Font.Bold = true;
                lOpp.Font.Size = 9;
                lOpp.Font.Name = "Arial";
                //lOpp.ForeColor = System.Drawing.Color.FromName("#525663");
                cellOpp.Controls.Add(lOpp);
                rowOpp.Cells.Add(cellOpp);

                // Nueva celda opcion
                TableCell cellOpp2 = new TableCell();
                cellOpp2.HorizontalAlign = HorizontalAlign.Left;
                RadioButtonList rl2 = new RadioButtonList();
                rl2.ID = "option" + oppPage.Tables[1].Rows[i]["id_option"].ToString();
                rl2.Items.Add("Unlimited");
                rl2.Items.Add("View Only");
                rl2.Items.Add("Restricted");

                // Diseño y aparencia de la opcion
                rl2.ForeColor = System.Drawing.Color.FromName("#525663");

                /*rl2.Items[0].Attributes.Add("onmouseover", "this.style.color='green';");
                rl2.Items[0].Attributes.Add("onmouseout", "this.style.color='#525663';");
                rl2.Items[1].Attributes.Add("onmouseover", "this.style.color='#FA7E02';");
                rl2.Items[1].Attributes.Add("onmouseout", "this.style.color='#525663';");
                rl2.Items[2].Attributes.Add("onmouseover", "this.style.color='red';");
                rl2.Items[2].Attributes.Add("onmouseout", "this.style.color='#525663';");*/
                rl2.RepeatDirection = RepeatDirection.Horizontal;

                cellOpp2.Controls.Add(rl2);
                //cellOpp2.BorderStyle = BorderStyle.Groove;
                //cellOpp2.BorderWidth = 2;
                rowOpp.Cells.Add(cellOpp2);

                Table tableG2 = null;
                if (oppPage.Tables[1].Rows[i]["name_section"].ToString().Equals("General"))
                {
                    tableG2 = (Table)Accordion1.FindControl("TableSecGeneral");
                }
                else
                {
                    tableG2 = (Table)Accordion1.FindControl("TableSec" + oppPage.Tables[1].Rows[i]["id_section"].ToString().Trim());
                }
                if (tableG2 != null)
                {
                    tableG2.Controls.Add(rowOpp);
                    if (i < (oppPage.Tables[1].Rows.Count - 1))
                        if (int.Parse(oppPage.Tables[1].Rows[i]["id_section"].ToString()) != int.Parse(oppPage.Tables[1].Rows[i + 1]["id_section"].ToString()))
                        {
                            rowOpp = new TableRow();
                            contOpp = 0;
                        }
                    if (int.Parse(oppPage.Tables[1].Rows[i]["orden"].ToString()) == 0)
                    {
                        contOpp = 0;
                    }
                }
            }
        }
       // return result;
    }

    public void newSection(string id_parent)
    {
        DataSet section = null;
        section = DB.getSectionPage(id_parent);
        //***************************** Section *************************************************
        if (true)
        {
            Table tableP = new Table();
            // Tamaño y alineacion de la tabla
            if (section.Tables[0].Rows.Count > 1)
                tableP.Width = 820;
            tableP.CellSpacing = 0;
            tableP.HorizontalAlign = HorizontalAlign.Left;
            // Nombre y estilo de la tabla
            tableP.BorderStyle = BorderStyle.Groove;
            tableP.BorderWidth = 2;
            tableP.Font.Bold = true;
            
            TableRow rowP = new TableRow();
            TableCell cellP = new TableCell();
            Accordion1.Panes.Clear();
            cellP.Controls.Add(Accordion1);
            rowP.Cells.Add(cellP);
            tableP.Controls.Add(rowP);

            TableRow rowP2 = new TableRow();
            TableCell cellP2 = new TableCell();
            cellP2.Controls.Add(tableP);
            rowP2.Cells.Add(cellP2);
            tablaPermit.Controls.Add(rowP2);
            
            if (section != null)
            {
                for (int j = 0; j < section.Tables[0].Rows.Count; j++)
                {
                    Table tableS = new Table();

                    if (section.Tables[0].Rows[j]["section_name"].ToString().Trim().Equals("General"))
                    {
                        tableS.ID = "TableSecGeneral";
                    }
                    else
                    {
                        tableS.ID = "TableSec" + section.Tables[0].Rows[j]["section_id"].ToString().Trim();
                    }
                    tableS.CellSpacing = 1;
                    tableS.HorizontalAlign = HorizontalAlign.Left;
                    /*tableS.BorderStyle = BorderStyle.Solid;
                    tableS.BorderColor = System.Drawing.Color.Red;
                    tableS.BorderWidth = 2;*/
                    if (section.Tables[0].Rows.Count > 1)
                        tableS.Width = 820;
                    else
                        tableS.Width = 410;
                    Label lsection = new Label();
                    lsection.ID = "ls" + section.Tables[0].Rows[j]["section_id"].ToString();
                    lsection.Text = section.Tables[0].Rows[j]["section_name"].ToString();
                    lsection.Font.Size = 10;
                    TableCell c1 = new TableCell();
                    c1.HorizontalAlign=HorizontalAlign.Left;
                    c1.Controls.Add(lsection);
                    TableRow r1 = new TableRow();
                    r1.Cells.Add(c1);
                    if (!section.Tables[0].Rows[j]["section_name"].ToString().Trim().Equals("General"))
                        tableS.Controls.Add(r1);

                    AjaxControlToolkit.AccordionPane ap = new AjaxControlToolkit.AccordionPane();
                    ap.ID = "AP" + section.Tables[0].Rows[j]["section_id"].ToString();
                    ap.Controls[0].Controls.Add(lsection);
                    ap.Controls[1].Controls.Add(tableS);
                    Accordion1.Panes.Add(ap);
                }
            }
        }
        else { section = null; }
    }

    public void mostrarPermisos(int tabla, DataSet dsclient)
    {
        ruser = (rUser)Session["userinfo"];
        if (ruser != null)
        {
            long useraccess = 0; long userrestricted = 0;
            Panel panelAccess = puppermit;
            
            for (int j = 0; j < dsclient.Tables[tabla].Rows.Count; j++)
            {
                string campo = "";
                if (tabla == 0) { campo = "id_page"; } else if (tabla == 1) { campo = "id_option"; }

                for (int k = 0; k < ruser.permisos.Count; k++)
                {
                    RE_GenericBean re = (RE_GenericBean)ruser.permisos[k];
                    //string aux1 = dsclient.Tables[tabla].Rows[j][campo].ToString();
                    //string aux2 = re.intC1.ToString();
                    //string aux3 = dsclient.Tables[1].Rows[j]["name_option"].ToString();
                    if (dsclient.Tables[tabla].Rows[j]["id_page"].ToString() == re.intC1.ToString())
                    {
                        long papp1 = long.Parse(dsclient.Tables[tabla].Rows[j]["permit"].ToString());
                        useraccess = re.int64C1;
                        userrestricted = re.int64C2;

                        long papp2 = papp1 & useraccess;
                        long papp3 = papp1 & userrestricted;
                        string rbID = "";
                        if (tabla == 0) { rbID = "page" + dsclient.Tables[0].Rows[j]["id_page"].ToString(); } else if (tabla == 1) { rbID = "option" + dsclient.Tables[1].Rows[j]["id_option"].ToString(); }
                        if (papp2 == papp1)
                        {
                            if (papp3 != papp1)
                            {
                                RadioButtonList rUnlimitted = ((RadioButtonList)Accordion1.FindControl(rbID));
                                if (rUnlimitted != null)
                                {
                                    rUnlimitted.Items[0].Selected = true;
                                    rUnlimitted.Items[0].Attributes.Add("style", "color:green");
                                }
                            }
                        }

                        if (papp1 != papp2)
                        {
                            if (papp1 != papp3)
                            {
                                RadioButtonList rViewOnly = ((RadioButtonList)Accordion1.FindControl(rbID));
                                if (rViewOnly != null)
                                {
                                    rViewOnly.Items[1].Selected = true;
                                    rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
                                }
                            }
                        }

                        if (papp1 == papp3)
                        {
                            RadioButtonList rRestricted = ((RadioButtonList)Accordion1.FindControl(rbID));
                            if (rRestricted != null)
                            {
                                rRestricted.Items[2].Selected = true;
                                rRestricted.Items[2].Attributes.Add("style", "color:red");
                            }
                        }
                        break;
                    }
                }
            }
        }

    }

    private string crearXMLPermisos()
    {
        string result = null;
        result += "<?xml version=\"1.0\" encoding=\"utf-8\"?><data><userid>" + userinfo.getUsuarioId() + "</userid>";

        result += "<OptionsAccess>";
        DataSet dsauxper = (DataSet)(Session["dsPermitPages"]);

        if (dsauxper != null)
        {
            for (int a = 0; a < dsauxper.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                long permisos = 0;
                RadioButtonList rUnlimitted = ((RadioButtonList)Accordion1.FindControl(pageID));
                if (rUnlimitted != null)
                {
                    if (rUnlimitted.Items[0].Selected == true)
                    {
                        permisos += long.Parse(dsauxper.Tables[0].Rows[a]["permit"].ToString());
                    }
                }

                for (int b = 0; b < dsauxper.Tables[1].Rows.Count; b++)
                {
                    if (dsauxper.Tables[1].Rows[b]["id_page"].ToString() == dsauxper.Tables[0].Rows[a]["id_page"].ToString())
                    {
                        string optionID = "option" + dsauxper.Tables[1].Rows[b]["id_option"].ToString();
                        RadioButtonList rUnlimitted2 = ((RadioButtonList)Accordion1.FindControl(optionID));
                        if (rUnlimitted2 != null)
                        {
                            if (rUnlimitted2.Items[0].Selected == true)
                            {
                                permisos += long.Parse(dsauxper.Tables[1].Rows[b]["permit"].ToString());
                            }
                        }
                    }
                }
                string tableID = "tabla" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                Table tableaux = ((Table)puppermit.FindControl(tableID));
                if (tableaux != null)
                {
                    result += "<pageID>" + dsauxper.Tables[0].Rows[a]["id_page"].ToString() + " </pageID>";
                    result += "<permit>" + permisos + "</permit>";
                }
            }
        }
        result += "</OptionsAccess>";

        result += "<RestrictedAccess>";
        if (dsauxper != null)
        {
            for (int a = 0; a < dsauxper.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                long permisos = 0;
                RadioButtonList rUnlimitted = ((RadioButtonList)Accordion1.FindControl(pageID));
                if (rUnlimitted != null)
                {
                    if (rUnlimitted.Items[2].Selected == true)
                    {
                        permisos += long.Parse(dsauxper.Tables[0].Rows[a]["permit"].ToString());
                    }
                }

                for (int b = 0; b < dsauxper.Tables[1].Rows.Count; b++)
                {
                    if (dsauxper.Tables[1].Rows[b]["id_page"].ToString() == dsauxper.Tables[0].Rows[a]["id_page"].ToString())
                    {
                        string optionID = "option" + dsauxper.Tables[1].Rows[b]["id_option"].ToString();
                        RadioButtonList rUnlimitted2 = ((RadioButtonList)Accordion1.FindControl(optionID));
                        if (rUnlimitted2 != null)
                        {
                            if (rUnlimitted2.Items[2].Selected == true)
                            {
                                permisos += long.Parse(dsauxper.Tables[1].Rows[b]["permit"].ToString());
                            }
                        }
                    }
                }
                string tableID = "tabla" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                Table tableaux = ((Table)puppermit.FindControl(tableID));
                if (tableaux != null)
                {
                    result += "<pageID>" + dsauxper.Tables[0].Rows[a]["id_page"].ToString() + " </pageID>";
                    result += "<permit>" + permisos + "</permit>";
                }
            }
        }
        result += "</RestrictedAccess></data>";
        return result;
    }

    private int fillclientprices()
    {
        int result=0;
        Table table;
        table = new Table();
        table.Width = 375;
        TableRow row= new TableRow();
        DataSet ds = DB.GetProducts();
        if (ds != null)
        {

            for (int i = 0; i <= (ds.Tables[0].Rows.Count - 1); i++)
            {

                if ((i % 5) == 0)
                {

                    row = new TableRow(); 
                    row.Width = 375; 
                    table.Controls.Add(row); 

                }
                TableCell cell = new TableCell();
                cell.Width = 75;
                TextBox text = new TextBox();       
                text.Width = 50;
                text.ForeColor = System.Drawing.Color.Gray;   
                int imas= i+1;
                text.ID = "clientprice" + imas;     
                text.CssClass = "txtForm";          
                Label label = new Label();
                label.ID = "lclientprice"+imas;
                label.Width = 75;
               // label.CssClass = "letraNegra13";    
                //label.Text = ds.Tables[0].Rows[i]["name"].ToString().ToUpper();
                label.Text = ds.Tables[0].Rows[i]["Initial"].ToString().ToUpper();
                label.ForeColor = System.Drawing.Color.FromName("#525663");
                label.Font.Bold = true;
                Label labelv = new Label();
                labelv.ID = "vclientprice" + imas;
                labelv.Width = 25;
                labelv.ForeColor = System.Drawing.Color.FromName("#525663");
                labelv.Text = ds.Tables[0].Rows[i]["Product_id"].ToString();
                labelv.Font.Bold = true;
                labelv.Visible = false;
                foreach (RE_GenericBean obj in userinfo.prices)
                {
                    string p = obj.intC1.ToString();
                    string pp = obj.strC1;
                    if (labelv.Text.Equals(p))
                    {
                        text.Text = Utility.formatNumber(double.Parse(pp.ToString()));
                    }
                }
                
                cell.Controls.Add(label);
                cell.Controls.Add(labelv);
                cell.Controls.Add(text);
                 
                row.Controls.Add(cell);            
                result++;

            }

        }

        PClientPrices.Controls.Add(table);
        Session["tbl_clientprices"] = table;
        tableclient = table;
        return result;
        
    }

    private void fillvendorprices()
    {
        Table table;
        table = new Table();
        table.BorderStyle= BorderStyle.None;
        table.BorderWidth= 0;
        table.Width = 375;
        TableRow row = new TableRow();
        DataSet ds = DB.GetAllProcess();
        if (ds != null)
        {

            for (int i = 0; i <= (ds.Tables[0].Rows.Count - 1); i++)
            {

                if ((i % 3) == 0)
                {

                    row = new TableRow();
                    row.Width = 375;
                    table.Controls.Add(row);

                }
                TableCell cell = new TableCell();
                TextBox text = new TextBox();
                text.Width = 50;
                int imas = i + 1;
                text.ID = "vendorprice" + imas;
                text.CssClass = "txtForm";
                text.ForeColor = System.Drawing.Color.Gray;
                Label label = new Label();
                label.ID = "lvendorprice" + imas;
                label.Width = 100;
                label.ForeColor = System.Drawing.Color.FromName("#525663");
                label.Font.Bold = true;
                label.Text = ds.Tables[0].Rows[i]["name"].ToString();

                Label labelv = new Label();
                labelv.ID = "vvendorprice" + imas;
                labelv.Width = 25;
                labelv.ForeColor = System.Drawing.Color.FromName("#525663");
                labelv.Text = ds.Tables[0].Rows[i]["process_id"].ToString();
                labelv.Font.Bold = true;
                labelv.Visible = false;
                foreach (RE_GenericBean obj in userinfo.prices)
                {
                    string p = obj.intC1.ToString();
                    string pp = obj.strC1;
                    if (labelv.Text.Equals(p))
                    {

                        string st=Utility.formatNumber(double.Parse(pp));
                        text.Text = st;
                        
                    }
                }

                if (chkVendor.Checked && (label.Text.ToUpper().Equals("PHOTOS EXT") || label.Text.ToUpper().Equals("PHOTOS INT") ) )
                {
                    cell.Controls.Add(label);
                    cell.Controls.Add(labelv);
                    cell.Controls.Add(text);
                    cell.Width = 125;
                    row.Controls.Add(cell);
                }


            }

        }

        PVendorPrices.Controls.Add(table);
        Session["tbl_vendorprices"] = table;
        tablevendor = table;

    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        return default(string);
    }

    protected void cbxcategoria_SelectedIndexChanged(object sender, EventArgs e)
    {
       

    }

    private void fillcounties()
    {
        ChkListCounties.Items.Clear(); 
        DataSet dst = DB.GetStates();
        if (dst != null)
            {

                for (int i = 0; i <= (dst.Tables[0].Rows.Count - 1); i++)
                {
                      string state = dst.Tables[0].Rows[i]["name"].ToString();
                      string initialstate= dst.Tables[0].Rows[i]["initial"].ToString();
                      
                    if (dst.Tables[0].Rows[i]["enable"].ToString().Equals("1"))
                    {

                      

                        DataSet dsc = DB.GetCounties(state);

                        if (dsc != null)
                        {

                            if (dsc.Tables[0].Rows.Count > 0)
                            {

                                for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                                {
                                    if (dsc.Tables[0].Rows[ii]["enable"].ToString().Equals("1"))
                                    {
                                        ListItem list = new ListItem();
                                        //nameItem = dsc.Tables[0].Rows[i]["name"].ToString();
                                        list.Value = dsc.Tables[0].Rows[ii][0].ToString();
                                        list.Text = dsc.Tables[0].Rows[ii]["name"].ToString()+". "+initialstate;
                                        foreach (RE_GenericBean obj in userinfo.counties)
                                        {
                                            string b = obj.intC1.ToString();
                                            if (list.Value.ToUpper().Equals(b))
                                            {
                                                list.Selected = true;
                                            }
                                        }
                                        ChkListCounties.Items.Add(list);

                          

                                    }

                                }
                            }
                            
                        }
                    }
                }
        }

       

    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked)
        {
            foreach (ListItem item in ChkListEmployee.Items)
            {
                item.Selected = true;
            }


        }
        else {
            foreach (ListItem item in ChkListEmployee.Items)
            {
                item.Selected = false;
            }
        
        }
    }
    protected void chkallcomp_CheckedChanged(object sender, EventArgs e)
    {

        if (chkallcomp.Checked)
        {
            foreach (ListItem item in ChkListCompanyAcc.Items)
            {
                item.Selected = true;
            }


        }
        else {
            foreach (ListItem item in ChkListCompanyAcc.Items)
            {
                item.Selected = false;
            }
        
        }
    }
    protected void all_CheckedChanged(object sender, EventArgs e)
    {

        if (all.Checked)
        {
            foreach (ListItem item in ChkListViewDe.Items)
            {
                item.Selected = true;
            }


        }
        else {
            foreach (ListItem item in ChkListViewDe.Items)
            {
                item.Selected = false;
            }
        
        }
    }
    protected void chklimitedall_CheckedChanged(object sender, EventArgs e)
    {
        /*chklimitedall0.Checked = false;
        chkrestrictedall.Checked = false;
        if (chklimitedall.Checked)
        {
            for (int i = 0; i < ChkListAccess.Items.Count; i++)
            {
                ChkListRestricted.Items[i].Selected = false;
                ChkListAccess.Items[i].Selected = true;
                ChkListAccessViewOnly.Items[i].Selected = false;
            }



        }
        else
        {
            for (int i = 0; i < ChkListAccess.Items.Count; i++)
            {
                if (!ChkListRestricted.Items[i].Selected & !ChkListAccessViewOnly.Items[i].Selected)
                {
                    ChkListAccess.Items[i].Selected = true;
                    chklimitedall.Checked = true;

                }

            }

        }*/
    }
    protected void chkrestrictedall_CheckedChanged(object sender, EventArgs e)
    {
        /*chklimitedall0.Checked = false;
        chklimitedall.Checked = false;
        if (chkrestrictedall.Checked)
        {
            for (int i = 0; i < ChkListRestricted.Items.Count; i++)
            {
                ChkListRestricted.Items[i].Selected = true;
                ChkListAccess.Items[i].Selected = false;
                ChkListAccessViewOnly.Items[i].Selected = false;
            }



        }
        else
        {
            for (int i = 0; i < ChkListRestricted.Items.Count; i++)
            {
                if (!ChkListAccess.Items[i].Selected & !ChkListAccessViewOnly.Items[i].Selected)
                {
                    ChkListRestricted.Items[i].Selected = true;
                    chkrestrictedall.Checked = true;

                }

            }

        }*/

    }
    protected void chkfilteall_CheckedChanged(object sender, EventArgs e)
    {
        if (chkfilteall.Checked)
        {
            foreach (ListItem item in chkfilterview.Items)
            {
                item.Selected = true;
            }


        }
        else {
            foreach (ListItem item in chkfilterview.Items)
            {
                item.Selected = false;
            }
        
        }
    }
    protected void chkcompanyall_CheckedChanged(object sender, EventArgs e)
    {
        if (chkcompanyall.Checked)
        {
            foreach (ListItem item in CKListCompanies.Items)
            {
                item.Selected = true;
            }


        }
        else
        {
            foreach (ListItem item in CKListCompanies.Items)
            {
                item.Selected = false;
            }

        }

    }
    protected void chkaccall_CheckedChanged(object sender, EventArgs e)
    {
        if (chkaccall.Checked)
        {
            foreach (ListItem item in CKListAccounts.Items)
            {
                item.Selected = true;
            }


        }
        else
        {
            foreach (ListItem item in CKListAccounts.Items)
            {
                item.Selected = false;
            }

        }
    }
    protected void chkstatesall_CheckedChanged(object sender, EventArgs e)
    {
        if (chkstatesall.Checked)
        {
            foreach (ListItem item in ChkListState.Items)
            {
                item.Selected = true;
            }


        }
        else
        {
            foreach (ListItem item in ChkListState.Items)
            {
                item.Selected = false;
            }

        }

    }
    protected void chkcountiesall_CheckedChanged(object sender, EventArgs e)
    {
        if (chkcountiesall.Checked)
        {
            foreach (ListItem item in ChkListCounties.Items)
            {
                item.Selected = true;
            }


        }
        else
        {
            foreach (ListItem item in ChkListCounties.Items)
            {
                item.Selected = false;
            }

        }

    }

    private bool comprobar()
    {
        bool result = false;
        return result;
    }

    private string crearXML() {
        string result = "";
        result += "<?xml version=\"1.0\" encoding=\"utf-8\"?><data><userid>" + userinfo.getUsuarioId() + "</userid>";
        if (chkClient.Checked) { result += "<group>1</group>"; }
        if (chkVendor.Checked) { result += "<group>2</group>"; }
        if (chkEmployee.Checked) { result += "<group>3</group>"; }
       
        result += "<perfiles>";
         foreach (ListItem item in ChkListEmployee.Items)
         {
             if (item.Selected){
                result += "<perfil>"+  item.Value.ToString()+" </perfil>";
             }
         }
        result += "</perfiles>";
        result += "<CompanyAccess>";
        foreach (ListItem item in ChkListCompanyAcc.Items)
        {
            if (item.Selected)
            {
                result += "<company>" + item.Value.ToString() + " </company>";
            }
            else {
                result += "<companyno>" + item.Value.ToString() + " </companyno>";
            }
        }
        result += "</CompanyAccess>";

        result += "<OptionsAccess>";
/*
        DataSet dsauxper = (DataSet)(Session["dsPermitPages"]);
        Panel panelAccess = Punaccess;

        if(dsauxper != null)
        {
            for (int a = 0; a < dsauxper.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                long permisos = 0;
                RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(pageID));
                if (rUnlimitted != null)
                {
                    if (rUnlimitted.Items[0].Selected == true)
                    {
                        permisos += long.Parse(dsauxper.Tables[0].Rows[a]["permit"].ToString());    
                    }
                }

                for (int b = 0; b < dsauxper.Tables[1].Rows.Count; b++)
                {
                    if (dsauxper.Tables[1].Rows[b]["id_page"].ToString() == dsauxper.Tables[0].Rows[a]["id_page"].ToString())
                    {
                        string optionID = "option" + dsauxper.Tables[1].Rows[b]["id_option"].ToString();
                        RadioButtonList rUnlimitted2 = ((RadioButtonList)panelAccess.FindControl(optionID));
                        if (rUnlimitted2 != null)
                        {
                            if (rUnlimitted2.Items[0].Selected == true)
                            {
                                permisos += long.Parse(dsauxper.Tables[1].Rows[b]["permit"].ToString());
                            }
                        }
                    }
                }

                result += "<pageID>" + dsauxper.Tables[0].Rows[a]["id_page"].ToString() + " </pageID>";
                result += "<permit>" + permisos + "</permit>";
            }
            
        }
 */
        result += "</OptionsAccess>";
        //SEtego vista del Cliente
        result += "<ViewDefault>";
        foreach (ListItem item in ChkListViewDe.Items)
        {
            if (item.Selected)
            {
                result += "<view>" + item.Value.ToString() + " </view>";
            }
            else
            {
                result += "<viewno>" + item.Value.ToString() + " </viewno>";
            }
        } 
        result += "</ViewDefault>";

        result += "<RestrictedAccess>";
/*
        if(dsauxper != null)
        {
            for (int a = 0; a < dsauxper.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                long permisos = 0;
                RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(pageID));
                if (rUnlimitted != null)
                {
                    if (rUnlimitted.Items[2].Selected == true)
                    {
                        permisos += long.Parse(dsauxper.Tables[0].Rows[a]["permit"].ToString());
                    }
                }

                for (int b = 0; b < dsauxper.Tables[1].Rows.Count; b++)
                {
                    if (dsauxper.Tables[1].Rows[b]["id_page"].ToString() == dsauxper.Tables[0].Rows[a]["id_page"].ToString())
                    {
                        string optionID = "option" + dsauxper.Tables[1].Rows[b]["id_option"].ToString();
                        RadioButtonList rUnlimitted2 = ((RadioButtonList)panelAccess.FindControl(optionID));
                        if (rUnlimitted2 != null)
                        {
                            if (rUnlimitted2.Items[2].Selected == true)
                            {
                                permisos += long.Parse(dsauxper.Tables[1].Rows[b]["permit"].ToString());
                            }
                        }
                    }
                }

                result += "<pageID>" + dsauxper.Tables[0].Rows[a]["id_page"].ToString() + " </pageID>";
                result += "<permit>" + permisos + "</permit>";
            }

        }
*/
        result += "</RestrictedAccess>";
       
        result += "<FilterView>";
        foreach (ListItem item in chkfilterview.Items)
        {
            if (item.Selected)
            {
                result += "<product>" + item.Value.ToString() + " </product>";
            }
            else {
                result += "<productno>" + item.Value.ToString() + " </productno>";
            }
        }
        result += "</FilterView>";

        result += "<ClientCompanies>";
        foreach (ListItem item in CKListCompanies.Items)
         {
             if (item.Selected){
                result += "<company>"+  item.Value.ToString()+" </company>";
             }
        }
        result += "</ClientCompanies>";

        result += "<ClientAccounts>";
        foreach (ListItem item in CKListAccounts.Items)
        {
            if (item.Selected)
            {
                result += "<account>" + item.Value.ToString() + " </account>";
            }
        }

        result += "</ClientAccounts>";

        result += "<VendorStates>";
        foreach (ListItem item in ChkListState.Items)
        {
            if (item.Selected)
            {
                result += "<state>" + item.Value.ToString() + "</state>";
            }
        }

        result += "</VendorStates>";
        result += "<VendorCounties>";
        foreach (ListItem item in ChkListCounties.Items)
        {
            if (item.Selected)
            {
                result += "<county>" + item.Value.ToString() + "</county>";
            }
        }

        result += "</VendorCounties>";

        result += "<ClientPrices>";
        foreach (RE_GenericBean obj in userinfo.perfiles)
        {
            if (obj.intC1 == 1) 
            {
                for (int i = 1; i < contclient+1; i++)
                {
                    Label label = (Label)tableclient.FindControl("vclientprice" + i);
                    TextBox text = (TextBox)tableclient.FindControl("clientprice" + i);
                    if (text!= null && !text.Text.Equals(""))
                    {
                        result += "<product>" + label.Text + "</product><price>" + text.Text + " </price>";
                    }

                }
            }
        }
        result += "</ClientPrices>";

        result += "<VendorPrices>";
        foreach (RE_GenericBean obj in userinfo.perfiles)
        {
            if (obj.intC1 != 1)
            {
                for (int i = 1; i < contvendor+1; i++)
                {
                    Label label = (Label)tablevendor.FindControl("vvendorprice" + i);
                    TextBox text = (TextBox)tablevendor.FindControl("vendorprice" + i);
                    if ( text!=null && !text.Text.Equals(""))
                    { 
                        
                        result += "<product>" + label.Text + "</product><price>" + text.Text + " </price>";
                    }

                }

            }
        }
        result += "</VendorPrices></data>";
       
        return result;

    }

    protected void ChkListRestricted_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void ChkListAccess_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    protected void ChkListAccessViewOnly_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    protected void chklimitedall0_CheckedChanged(object sender, EventArgs e)
    {
        /*chklimitedall0.Checked = false;
        chkrestrictedall.Checked = false;
        if (chklimitedall.Checked)
        {
            for (int i = 0; i < ChkListAccess.Items.Count; i++)
            {
                ChkListRestricted.Items[i].Selected = false;
                ChkListAccess.Items[i].Selected = true;
                ChkListAccessViewOnly.Items[i].Selected = false;
            }



        }
        else
        {
            for (int i = 0; i < ChkListAccess.Items.Count; i++)
            {
                if (!ChkListRestricted.Items[i].Selected & !ChkListAccessViewOnly.Items[i].Selected)
                {
                    ChkListAccess.Items[i].Selected = true;
                    chklimitedall.Checked = true;

                }

            }

        }*/
    }

    

    protected void ChkListEmployee_SelectedIndexChanged(object sender, EventArgs e)
    {
       
       // MessageBox.Show("Welcome to the Wonderful World of Visual C#");
        CheckBoxList ch= (CheckBoxList)sender;
        DataSet dsclient=null;
        int sele= ch.SelectedIndex;
       
        int rolindex = (int)Session["rolindex"];
         
       
         for (int i = 0; i < ch.Items.Count; i++ ) {
           
            if (ch.Items[i].Selected == true ) {

                if (rolindex != -1)
                {
                    string oldvalue = ch.Items[rolindex].Value;

                    if (oldvalue != ch.Items[i].Value.ToString())
                    {
                        ch.SelectedValue = ch.Items[i].Value;
                    }
                }
            }
        
        }
         if (ch.Text.Equals(""))
         {
             ch.SelectedIndex = (int)Session["rolindex"];
         }
         else {
             Session["rolindex"] = ch.SelectedIndex;
         }

        long rolaccess = 0, rolrestricted = 0, useraccess = 0, userrestricted = 0;
        rolaccess = long.Parse(userinfo.getPermiso().ToString());
        rolrestricted = long.Parse(userinfo.restrictedrol.ToString());
        useraccess = 0;
        userrestricted = 0;
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        string rxml = service.getAppPages(1);
        
        if (!rxml.Equals("") || rxml != null)
        {
                dsclient = Utility.getPermitPage(rxml);
        }


        Panel panelAccess = Punaccess;
      
        string resp = service.getPerfilPerRes(int.Parse(ch.Text.ToString()), "1");
        DataSet re=  Utility.PharseXML_PerRes(resp);
        

        for (int j = 0; j < dsclient.Tables[0].Rows.Count; j++)
        {
            useraccess = 0; userrestricted = 0;
            for (int k = 0; k < re.Tables[0].Rows.Count; k++)
            {
                if (dsclient.Tables[0].Rows[j]["id_page"].ToString() == re.Tables[0].Rows[k]["page_id"].ToString())
                {
                    useraccess = Int64.Parse(re.Tables[0].Rows[k]["per_permiso"].ToString());
                    userrestricted = Int64.Parse(re.Tables[0].Rows[k]["per_restricted"].ToString());
                    break;
                }
            }
            long papp1 = long.Parse(dsclient.Tables[0].Rows[j]["permit"].ToString());
            long papp2 = papp1 & useraccess;
            long papp3 = papp1 & userrestricted;
            if (papp2 == papp1)
            {
                if (papp3 != papp1)
                {
                    string pageID = "page" + dsclient.Tables[0].Rows[j]["id_page"].ToString();
                    RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(pageID));
                    if (rUnlimitted != null)
                    {
                        rUnlimitted.Items[0].Selected = true;
                        rUnlimitted.Items[0].Attributes.Add("style", "color:green");
                        rUnlimitted.Items[1].Selected = false;
                        rUnlimitted.Items[1].Attributes.Add("style", "color:black");
                        rUnlimitted.Items[2].Selected = false;
                        rUnlimitted.Items[2].Attributes.Add("style", "color:black");
                    }
                }
            }

            if (papp1 != papp2)
            {
                if (papp1 != papp3)
                {
                    string pageID = "page" + dsclient.Tables[0].Rows[j]["id_page"].ToString();
                    RadioButtonList rViewOnly = ((RadioButtonList)panelAccess.FindControl(pageID));
                    if (rViewOnly != null)
                    {
                        rViewOnly.Items[1].Selected = true;
                        rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
                        rViewOnly.Items[0].Selected = false;
                        rViewOnly.Items[0].Attributes.Add("style", "color:black");
                        rViewOnly.Items[2].Selected = false;
                        rViewOnly.Items[2].Attributes.Add("style", "color:black");
                    }
                }
            }

            if (papp1 == papp3)
            {
                string pageID = "page" + dsclient.Tables[0].Rows[j]["id_page"].ToString();
                RadioButtonList rRestricted = ((RadioButtonList)panelAccess.FindControl(pageID));
                if (rRestricted != null)
                {
                    rRestricted.Items[2].Selected = true;
                    rRestricted.Items[2].Attributes.Add("style", "color:red");
                    rRestricted.Items[0].Selected = false;
                    rRestricted.Items[0].Attributes.Add("style", "color:black");
                    rRestricted.Items[1].Selected = false;
                    rRestricted.Items[1].Attributes.Add("style", "color:black");
                }
            }
        }
        for (int j = 0; j < dsclient.Tables[1].Rows.Count; j++)
        {
            useraccess = 0; userrestricted = 0;
            for (int k = 0; k < re.Tables[0].Rows.Count; k++)
            {
                if (dsclient.Tables[1].Rows[j]["id_page"].ToString() == re.Tables[0].Rows[k]["page_id"].ToString())
                {
                    useraccess = Int64.Parse(re.Tables[0].Rows[k]["per_permiso"].ToString());
                    userrestricted = Int64.Parse(re.Tables[0].Rows[k]["per_restricted"].ToString());
                    break;
                }
            }
            long papp1 = long.Parse(dsclient.Tables[1].Rows[j]["permit"].ToString());
            long papp2 = papp1 & useraccess;
            long papp3 = papp1 & userrestricted;
            
            if (papp2 == papp1)
            {

                if (papp3 != papp1)
                {
                    string optionID = "option" + dsclient.Tables[1].Rows[j]["id_option"].ToString();
                    RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(optionID));
                    if (rUnlimitted != null)
                    {
                        rUnlimitted.Items[0].Selected = true;
                        rUnlimitted.Items[0].Attributes.Add("style", "color:green");
                        rUnlimitted.Items[1].Selected = false;
                        rUnlimitted.Items[1].Attributes.Add("style", "color:#525663");
                        rUnlimitted.Items[2].Selected = false;
                        rUnlimitted.Items[2].Attributes.Add("style", "color:#525663");
                    }
                }
            }

            if (papp1 != papp2)
            {
                if (papp1 != papp3)
                {
                    string optionID = "option" + dsclient.Tables[1].Rows[j]["id_option"].ToString();
                    RadioButtonList rViewOnly = ((RadioButtonList)panelAccess.FindControl(optionID));
                    if (rViewOnly != null)
                    {
                        rViewOnly.Items[1].Selected = true;
                        rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
                        rViewOnly.Items[0].Selected = false;
                        rViewOnly.Items[0].Attributes.Add("style", "color:#525663");
                        rViewOnly.Items[2].Selected = false;
                        rViewOnly.Items[2].Attributes.Add("style", "color:#525663");
                    }
                }
            }

            if (papp1 == papp3)
            {
                string optionID = "option" + dsclient.Tables[1].Rows[j]["id_option"].ToString();
                RadioButtonList rRestricted = ((RadioButtonList)panelAccess.FindControl(optionID));
                if (rRestricted != null)
                {
                    rRestricted.Items[2].Selected = true;
                    rRestricted.Items[2].Attributes.Add("style", "color:red");
                    rRestricted.Items[0].Selected = false;
                    rRestricted.Items[0].Attributes.Add("style", "color:#525663");
                    rRestricted.Items[1].Selected = false;
                    rRestricted.Items[1].Attributes.Add("style", "color:#525663");
                }
            }
        }
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet dsclien = (DataSet)Session["dsPermitPages"];
        Panel panelAccess = puppermit;
        if (allaccess.Items[0].Selected == true)
        {
            allaccess.Items[0].Attributes.Add("style", "color:green");
            for (int a = 0; a < dsclien.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsclien.Tables[0].Rows[a]["id_page"].ToString();
                //string pageID2 = "page_" + dsclien.Tables[0].Rows[a]["id_page"].ToString();
                RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(pageID));
                if (rUnlimitted != null)
                {
                    //crearPagPermisos(pageID2);
                    rUnlimitted.Items[2].Selected = false;
                    rUnlimitted.Items[2].Attributes.Add("style", "color:black");
                    rUnlimitted.Items[1].Selected = false;
                    rUnlimitted.Items[1].Attributes.Add("style", "color:black");
                    rUnlimitted.Items[0].Selected = true;
                    rUnlimitted.Items[0].Attributes.Add("style", "color:green");
                }
            }
            for (int b = 0; b < dsclien.Tables[1].Rows.Count; b++)
            {
                string optionID = "option" + dsclien.Tables[1].Rows[b]["id_option"].ToString();
                RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(optionID));
                if (rUnlimitted != null)
                {
                    rUnlimitted.Items[2].Selected = false;
                    rUnlimitted.Items[2].Attributes.Add("style", "color:#525663");
                    rUnlimitted.Items[1].Selected = false;
                    rUnlimitted.Items[1].Attributes.Add("style", "color:#525663");
                    rUnlimitted.Items[0].Selected = true;
                    rUnlimitted.Items[0].Attributes.Add("style", "color:green");
                }
            }
        }
        if (allaccess.Items[1].Selected == true)
        {
            allaccess.Items[1].Attributes.Add("style", "color:#FA7E02");
            for (int a = 0; a < dsclien.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsclien.Tables[0].Rows[a]["id_page"].ToString();
                RadioButtonList rViewOnly = ((RadioButtonList)panelAccess.FindControl(pageID));
                if (rViewOnly != null)
                {
                    rViewOnly.Items[2].Selected = false;
                    rViewOnly.Items[2].Attributes.Add("style", "color:black");
                    rViewOnly.Items[0].Selected = false;
                    rViewOnly.Items[0].Attributes.Add("style", "color:black");
                    rViewOnly.Items[1].Selected = true;
                    rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
                }
            }
            for (int b = 0; b < dsclien.Tables[1].Rows.Count; b++)
            {
                string optionID = "option" + dsclien.Tables[1].Rows[b]["id_option"].ToString();
                RadioButtonList rViewOnly = ((RadioButtonList)panelAccess.FindControl(optionID));
                if (rViewOnly != null)
                {
                    rViewOnly.Items[2].Selected = false;
                    rViewOnly.Items[2].Attributes.Add("style", "color:#525663");
                    rViewOnly.Items[0].Selected = false;
                    rViewOnly.Items[0].Attributes.Add("style", "color:#525663");
                    rViewOnly.Items[1].Selected = true;
                    rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
                }
            }
        }
        if (allaccess.Items[2].Selected == true)
        {
            allaccess.Items[2].Attributes.Add("style", "color:red");
            for (int a = 0; a < dsclien.Tables[0].Rows.Count; a++)
            {
                string pageID = "page" + dsclien.Tables[0].Rows[a]["id_page"].ToString();
                RadioButtonList rRestricted = ((RadioButtonList)panelAccess.FindControl(pageID));
                if (rRestricted != null)
                {
                    rRestricted.Items[0].Selected = false;
                    rRestricted.Items[0].Attributes.Add("style", "color:black");
                    rRestricted.Items[1].Selected = false;
                    rRestricted.Items[1].Attributes.Add("style", "color:black");
                    rRestricted.Items[2].Selected = true;
                    rRestricted.Items[2].Attributes.Add("style", "color:red");
                }
            }
            for (int b = 0; b < dsclien.Tables[1].Rows.Count; b++)
            {
                string optionID = "option" + dsclien.Tables[1].Rows[b]["id_option"].ToString();
                RadioButtonList rRestricted = ((RadioButtonList)panelAccess.FindControl(optionID));
                if (rRestricted != null)
                {
                    rRestricted.Items[0].Selected = false;
                    rRestricted.Items[0].Attributes.Add("style", "color:#525663");
                    rRestricted.Items[1].Selected = false;
                    rRestricted.Items[1].Attributes.Add("style", "color:#525663");
                    rRestricted.Items[2].Selected = true;
                    rRestricted.Items[2].Attributes.Add("style", "color:red");
                }
            }
        }
    }



    protected void lnk_save_Click(object sender, EventArgs e)
    {
        string xml = crearXML();
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        //referencia1.Service service = new referencia1.Service();
        int resp = service.setDataUser(xml);
    }
    protected void lnk_savegroup_Click(object sender, EventArgs e)
    {
        if (chkEmployee.Checked || chkVendor.Checked)
        {
            ChkListEmployee.Enabled = true;
        }
        else
        {
            ChkListEmployee.Enabled = false;
        }
    }
    protected void lnk_addnew_Click(object sender, EventArgs e)
    {
        lerror.Text = "";
        lerror2.Text = "";
        foreach (Control control in PUserType.Controls)
        {
            if (control is Panel)
            {
                if (!control.ID.Equals("PAdd"))
                {
                    control.Visible = false;
                }
            }
        }
        if (!cbxcategoria.SelectedValue.ToUpper().Equals("SELECT ONE"))
        {
            switch (cbxcategoria.SelectedValue.ToUpper())
            {

                case "TYPE EMPLOYEE":
                    TEmployye.Visible = true;
                    break;
                case "COMPANY":
                    PCompany.Visible = true;
                    break;
                case "ACCOUNT":
                    PAccount.Visible = true;
                    break;
                case "STATE":
                    Pstate.Visible = true;
                    break;
                case "COUNTY":
                    Pcounty.Visible = true;
                    txtcounty.Visible = true;
                    txtcounty_TextBoxWatermarkExtender.WatermarkText = "County Name";
                    txtcounty_TextBoxWatermarkExtender.Enabled = true;



                    break;

            }
        }
        else
        {
            lerror.Text = "Invalid item. ";

        }
    }
    protected void lnk_addtemployee_Click(object sender, EventArgs e)
    {
        lerror2.Text = "";
        if (!txtemployee.Text.Equals(""))
        {
            string sxml = "";
            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><name>";
            sxml = sxml + txtemployee.Text;
            sxml = sxml + "</name><catalogo>1</catalogo></datos_registro>";
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            int resp = service.RegistraTypeEmployee(sxml);
            if (resp == 1)
            {
                fillperfiles();
                TEmployye.Visible = false;
                txtemployee.Text = "";
            }
            else
            {
                lerror2.Text = "Type Employee no added. Try Again.";
            }
        }
        else
        {
            lerror2.Text = "Invalid name for new type employee";
        }
    }
    protected void lnk_addcompany_Click(object sender, EventArgs e)
    {
        lerror2.Text = "";
        if (!txtcompany.Text.Equals(""))
        {
            string sxml = "";
            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><name>";
            sxml = sxml + txtcompany.Text;
            sxml = sxml + "</name><estado>1</estado></datos_registro>";
            int resp;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            resp = service.RegistraCompany(sxml);
            if (resp == 1)
            {
                fillcompanies();
                PCompany.Visible = false;
                txtcompany.Text = "";

            }
            else
            {
                lerror2.Text = "Company no added. Try Again.";
            }

        }
        else
        {
            lerror2.Text = "Invalid name for new type employee";
        }
    }
    protected void lnk_addaccount_Click(object sender, EventArgs e)
    {
        lerror2.Text = "";
        if (!txtaccount.Text.Equals("") && !txtaccinitial.Text.Equals(""))
        {
            string sxml;
            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><name>";
            sxml = sxml + txtaccount.Text;
            sxml = sxml + "</name><initial>" + txtaccinitial.Text + "</initial><estado>1</estado></datos_registro>";
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            int resp = service.RegistraAccount(sxml);
            if (resp == 1)
            {
                fillaccounts();
                txtaccount.Text = "";
                txtaccinitial.Text = "";
                PAccount.Visible = false;
            }
            else
            {
                lerror2.Text = "Account no added";

            }

        }
        else
        {
            lerror2.Text = "Name and Initial are Required!";

        }
    }
    protected void lnk_addstate_Click(object sender, EventArgs e)
    {
        lerror2.Text = "";
        int resp = 0;
        if (!txtstate.Text.Equals("") && !txtstateinitial.Text.Equals(""))
        {
            resp = DB.addState(txtstate.Text, txtstateinitial.Text);
            if (resp == 1)
            {
                txtstate.Text = "";
                txtstateinitial.Text = "";
                Pstate.Visible = false;
                fillstates();


            }
            else if (resp == 3)
            {
                lerror2.Text = "State already exists!";
            }
            else
            {
                lerror2.Text = "Error. State no Added. Try Again";
            }
        }
        else
        {
            lerror2.Text = "State name and initial are requerid!";
        }
    }
    protected void lnk_addcounty_Click(object sender, EventArgs e)
    {
        lerror2.Text = "";
        int resp = 0;
        if (!txtcounty.Text.Equals("") && !txtcountyinitial.Text.Equals("") && cbxstate.SelectedIndex != 0)
        {
            resp = DB.addCounty(cbxstate.SelectedValue.ToString(), txtcounty.Text, txtcountyinitial.Text);
            if (resp == 1)
            {
                txtcounty.Text = "";
                txtcountyinitial.Text = "";
                Pcounty.Visible = false;
                fillcounties();


            }
            else if (resp == 3)
            {
                lerror2.Text = "County already exists!";
            }
            else
            {
                lerror2.Text = "Error. County no Added. Try Again";
            }

        }
        else
        {
            lerror2.Text = "State , County name and initial are requerid!";

        }
    }
    protected void lnk_deactivate_Click(object sender, EventArgs e)
    {
        com.bpophotos.www.Service service=  null;
        try
        {
            service = new com.bpophotos.www.Service();
            userinfo = (rUser)Session["userinfo"];
            if (userinfo != null)
            {
                string resp = service.DesactivateUser(userinfo.getUsuarioId());
                if (resp.Equals("1"))
                {
                    lnk_activate.Visible = true;
                    lnk_deactivate.Visible = false;
                }
            }
        }
        catch (Exception ee)
        {
            Utility.writeError("Error al desactivar usuario " + ee.Message + ee.StackTrace);
        }
        finally {
            if (service != null) {
                service.Dispose();
            }
        }
    }
    protected void lnk_activate_Click(object sender, EventArgs e)
    {
        com.bpophotos.www.Service service = null;
        try
        {
            service = new com.bpophotos.www.Service();
            userinfo = (rUser)Session["userinfo"];
            if (userinfo != null) {
                string resp = service.ActivateUser(userinfo.getUsuarioId());
                if (resp.Equals("1"))
                {
                    lnk_activate.Visible = false;
                    lnk_deactivate.Visible = true;
                }
            }
        }
        catch (Exception ee)
        {
            Utility.writeError("Error al desactivar usuario " + ee.Message + ee.StackTrace);
        }
        finally
        {
            if (service != null)
            {
                service.Dispose();
            }
        }
    }

    protected void lnk_savePermit_Click(object sender, EventArgs e)
    {
        string xml = crearXMLPermisos();
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        int resp = service.setDataUser(xml);

        puser = Request.QueryString["Id"].ToString();
        string info = service.getTodosDatosUsuario(puser);
        userinfo = Utility.getRUser(info);
        if (userinfo != null)
            Session["userinfo"] = userinfo;
        userinfo = (rUser)(Session["userinfo"]);
    }
    protected void lnk_back_Click(object sender, EventArgs e)
    {
        rpage.intC1 = 0; rpage.intC2 = 0;
        Session["rpermitpage"] = rpage;
        Response.Redirect(Request.UrlReferrer.ToString());
    }
}